<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="办理点编号" prop="siteno">
      <el-input v-model="dataForm.siteno" placeholder="办理点编号" maxlength="4"></el-input>
    </el-form-item>
    <el-form-item label="办理点名称" prop="sitename">
      <el-input v-model="dataForm.sitename" placeholder="办理点名称"></el-input>
    </el-form-item>
    <el-form-item label="办理点地址" prop="siteaddr">
      <el-input v-model="dataForm.siteaddr" placeholder="办理点地址"></el-input>
    </el-form-item>
    <el-form-item label="办理点所属区域" prop="region">
      <el-select v-model="dataForm.region" placeholder="办理点所属区域" style="width:100%">
          <el-option
            v-for="item in dataForm.regionList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
      </el-select>
    </el-form-item>
        <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          seqno: 0,
          siteno: '',
          sitename: '',
          siteaddr: '',
          region: '',
          regionList: [],
          remark: ''
        },
        dataRule: {
          siteno: [
            { required: true, message: '办理点编号不能为空', trigger: 'blur' }
          ],
          sitename: [
            { required: true, message: '办理点名称不能为空', trigger: 'blur' }
          ],
          siteaddr: [
            { required: true, message: '办理点地址不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '办理点所属区域不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.seqno = id || 0
        this.visible = true

        this.$http({
          url: this.$http.adornUrl(`/bdc/bdcsiteinfo/region/list`),
              method: 'get',
              params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.regionList = data.regionList
            }
        })

        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.seqno) {
            this.$http({
              url: this.$http.adornUrl(`/bdc/bdcsiteinfo/info/${this.dataForm.seqno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.siteno = data.bdcSiteinfo.siteno
                this.dataForm.sitename = data.bdcSiteinfo.sitename
                this.dataForm.siteaddr = data.bdcSiteinfo.siteaddr
                this.dataForm.region = data.bdcSiteinfo.region
                this.dataForm.remark = data.bdcSiteinfo.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bdc/bdcsiteinfo/${!this.dataForm.seqno ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'seqno': this.dataForm.seqno || undefined,
                'siteno': this.dataForm.siteno,
                'sitename': this.dataForm.sitename,
                'siteaddr': this.dataForm.siteaddr,
                'region': this.dataForm.region,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
